from sqlalchemy.orm import Session
from . import models, schemas

def get_menu_event(db: Session, menu_event_id: int):
    return db.query(models.MenuEvent).filter(models.MenuEvent.id == menu_event_id).first()

def get_menu_events(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.MenuEvent).offset(skip).limit(limit).all()

def create_menu_event(db: Session, menu_event: schemas.MenuEventCreate):
    db_menu_event = models.MenuEvent(**menu_event.dict())
    db.add(db_menu_event)
    db.commit()
    db.refresh(db_menu_event)
    return db_menu_event

def update_menu_event(db: Session, menu_event_id: int, menu_event: schemas.MenuEventUpdate):
    db_menu_event = db.query(models.MenuEvent).filter(models.MenuEvent.id == menu_event_id).first()
    if db_menu_event:
        for var, value in vars(menu_event).items():
            setattr(db_menu_event, var, value) if value else None
        db.add(db_menu_event)
        db.commit()
        db.refresh(db_menu_event)
    return db_menu_event

def delete_menu_event(db: Session, menu_event_id: int):
    db_menu_event = db.query(models.MenuEvent).filter(models.MenuEvent.id == menu_event_id).first()
    if db_menu_event:
        db.delete(db_menu_event)
        db.commit()
    return db_menu_event